//
//  WBNewFeatureView.swift
//  WeiBoApp
//
//  Created by CrazyHacker on 16/7/4.
//  Copyright © 2016年 CrazyHacker. All rights reserved.
//

import UIKit

class WBNewFeatureView: UIView {

    @IBOutlet weak var ScrollView: UIScrollView!
    
    @IBOutlet weak var EnterButton: UIButton!

    @IBOutlet weak var PageControl: UIPageControl!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    backgroundColor = #colorLiteral(red: 0.9346159697, green: 0.6284804344, blue: 0.1077284366, alpha: 1)
        
    }
    
    // MARK: - 进入微博
    @IBAction func enterWiBo(_ sender: AnyObject) {
        self.removeFromSuperview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        EnterButton.isHidden = true
        let count = 4
        let rect = UIScreen.main().bounds
        
        for i in 0..<4 {
            let imageName = "new_feature_\(i + 1)"
            let image: UIImageView = UIImageView(image: UIImage(named: imageName))
            image.frame = rect.offsetBy(dx: CGFloat(i) * rect.size.width, dy: 0)
            ScrollView.addSubview(image)
        }
        
        ScrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        
        self.ScrollView.bounces = false
        self.ScrollView.isPagingEnabled = true
        self.ScrollView.showsVerticalScrollIndicator = false
        self.ScrollView.showsHorizontalScrollIndicator = false
        self.ScrollView.delegate = self
    }
    
}

extension WBNewFeatureView: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(ScrollView.contentOffset.x / UIScreen.main().bounds.width)
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        
        self.EnterButton.isHidden = (page != scrollView.subviews.count - 1)
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        EnterButton.isHidden = true
        let page = Int(scrollView.contentOffset.x / UIScreen.main().bounds.width + 0.5)
        self.PageControl.currentPage = page
        self.PageControl.isHidden = (page == scrollView.subviews.count)
        
    }
    
}

extension WBNewFeatureView {
   class func wbNewFeatureView()->WBNewFeatureView {
        let nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
    v.frame = UIScreen.main().bounds
        return v
    }
}
